package com.polaris.live.adapter.live

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.polaris.live.R
import com.polaris.live.adapter.HomeUserHeadAdapter
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.ext.autoDp
import com.polaris.live.common.util.AppCodeUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterFollowUserItemBinding
import com.polaris.live.databinding.LayoutLiveFollowNoDataBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.utils.HomeHeadItemDecoration
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.utils.state.UserStateUtils
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * HomeFollowUserAdapter 关注的主播
 *
 * @author Created by 天晴 on 2025/1/3/005 11:41
 */
class HomeFollowUserAdapter : BaseAdapter<Any, BaseViewHolder>(0) {

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> TYPE_NORMAL
            is String -> TYPE_DATA
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            TYPE_DATA -> DontHavaViewHolder(
                LayoutLiveFollowNoDataBinding.inflate(layoutInflater, parent, false)
            )

            TYPE_NORMAL -> UserViewHolder(
                AdapterFollowUserItemBinding.inflate(layoutInflater, parent, false)
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is UserViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is DontHavaViewHolder -> if (item is String) holder.bind(item)
        }
    }

    class DontHavaViewHolder(
        binding: LayoutLiveFollowNoDataBinding,
    ) : BaseBindingViewHolder<LayoutLiveFollowNoDataBinding, String>(binding) {

        override fun bind(item: String) {
            mBinding.apply {

            }
        }
    }

    class UserViewHolder(
        binding: AdapterFollowUserItemBinding,
    ) :
        BaseBindingViewHolder<AdapterFollowUserItemBinding, DisplayUserBean>(binding) {

        override fun bind(item: DisplayUserBean) {
            mBinding.apply {
                avFv.setFrameString(item.base.avatarFrame)
                avatar.setImageUrl(item.base.avatar)

                Glide.with(ivRoomTypeCover.context).load(ImageUtils.imgPath(item.stateBackground))
                    .transform(RoundedCorners(8.autoDp)).into(ivRoomTypeCover)
                tvNickName.text = item.roomName

                userState(mBinding, item)
                tvUserDetail.text = item.roomWelcomeSpeech ?: " "
                tvUserNum.text = item.stayUser.toString()

                when (item.roomType) {
                    LiveConst.RoomType.NORMAL -> {
                        roomTag.visibility = View.GONE
                        roomTag.setImageResource(0)
                    }

                    LiveConst.RoomType.AGENCY -> {  //公会
                        roomTag.visibility = View.VISIBLE
                        roomTag.setImageResource(if (AppCodeUtils.isAR()) R.drawable.family_tag_ar else R.drawable.family_tag)
                    }

                    LiveConst.RoomType.OFFICIAL -> { //官方
                        roomTag.visibility = View.VISIBLE
                        roomTag.setImageResource(if (AppCodeUtils.isAR()) R.drawable.sala_tag_ar else R.drawable.sala_tag)
                    }

                    else -> {
                        roomTag.visibility = View.GONE
                        roomTag.setImageResource(0)
                    }
                }
                vipView.setVipLevel(item.base.vipLevel)
                val roomType = item.base.liveType
                if (roomType == LiveConst.Type.PARTY_GAME_LIVE) {
                    setGameLevel(mBinding, item)
                } else {
                    widgetGameLevel.visibility = View.GONE
                    widgetLevel.setCharmsLevel(item.base.charms)
                }

                headRecycler.apply {
                    adapter = HomeUserHeadAdapter().apply { setList(item.siteUsers?.asReversed()) }
                    layoutManager =
                        LinearLayoutManager(mBinding.root.context, LinearLayoutManager.HORIZONTAL, false).apply {
                            reverseLayout = true
                            stackFromEnd = true
                        }
                    itemAnimator = null
                    addItemDecoration(HomeHeadItemDecoration())
                }

            }
        }

        private fun setGameLevel(mBinding: AdapterFollowUserItemBinding, item: DisplayUserBean) {
            mBinding.apply {
                widgetLevel.visibility = View.GONE
                val gameLevel = item.gameLevel ?: 0
                if (gameLevel == 0) {
                    widgetGameLevel.visibility = View.GONE
                } else {
                    widgetGameLevel.visibility = View.VISIBLE
                }
                tvLevel.text = "Lv${gameLevel}"
            }
        }

        private fun userState(binding: AdapterFollowUserItemBinding, item: DisplayUserBean) {
            item.base.let { userBasic ->
                when (val state = UserStateUtils.resolveUserState(userBasic)) {
                    UserStateUtils.USER_STATE_ONLINE, UserStateUtils.USER_STATE_OFFLINE, UserStateUtils.USER_STATE_BUSY -> {
                        binding.vState.visibility = View.VISIBLE
                        binding.ivStateLive.visibility = View.GONE
                        when (state) {
                            UserStateUtils.USER_STATE_ONLINE -> {
                                binding.vState.setBackgroundResource(R.drawable.ic_user_space)
                            }

                            UserStateUtils.USER_STATE_OFFLINE -> {
                                binding.vState.setBackgroundResource(R.drawable.shape_session_state_offline)
                            }

                            UserStateUtils.USER_STATE_BUSY -> {
                                binding.vState.setBackgroundResource(R.drawable.shape_session_state_busy)
                            }

                            else -> {
                                binding.vState.visibility = View.GONE
                                binding.ivStateLive.visibility = View.GONE
                            }
                        }
                    }

                    UserStateUtils.USER_STATE_LIVE, UserStateUtils.USER_STATE_MATCH, UserStateUtils.USER_STATE_PARTY, UserStateUtils.USER_STATE_GAME, UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                        binding.vState.visibility = View.GONE
                        binding.ivStateLive.visibility = View.VISIBLE
                        when (state) {
                            UserStateUtils.USER_STATE_LIVE -> {
                                binding.ivStateLive.setImageResource(R.drawable.icon_party_live_1)
                            }

                            UserStateUtils.USER_STATE_MATCH -> {
                                binding.ivStateLive.setImageResource(R.drawable.icon_list_match)
                            }
                            //派对
                            UserStateUtils.USER_STATE_PARTY -> {
                                binding.ivStateLive.setImageResource(R.drawable.icon_party)
                            }
                            //游戏房
                            UserStateUtils.USER_STATE_GAME -> {
                                binding.ivStateLive.setImageResource(R.drawable.icon_party_game_1)
                            }

                            UserStateUtils.USER_STATE_PARTY_VIDEO -> {
                                binding.ivStateLive.setImageResource(R.drawable.icon_party_video)
                            }

                            else -> {
                                binding.vState.visibility = View.GONE
                                binding.ivStateLive.visibility = View.GONE
                            }
                        }
                    }

                    else -> {
                        binding.vState.visibility = View.GONE
                        binding.ivStateLive.visibility = View.GONE
                    }
                }
            }
        }
    }

    companion object {

        const val TYPE_DATA = 0
        const val TYPE_NORMAL = 1
    }
}